// pages/goods/my-trading/my-trading.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    remark:'',
    active: 0,
    good_list:[],
    result:0,
    showConfirm:false,
    pageNum:1,
    pageTotal:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      remark:options.remark,
      active: Number(options.active)
    })
    this.getTradingList()
  },
  onChange(event){
    this.setData({
      result:0,
      active:event.detail.index,
      good_list:[],
      pageNum:1
    })
    this.getTradingList()
  },

  chooseMall(event) {
    this.setData({
      result: event.detail
    })
  },
  noop() {},
  toggle(event) {
    this.setData({
      result: event.currentTarget.dataset.id
    })
    return
    const { index } = event.currentTarget.dataset;
    const checkbox = this.selectComponent(`.checkboxes-${index}`);
    checkbox.toggle();
  },
  // 获取交易列表
  getTradingList(){
    let _this = this
    wx.showLoading({
      title: '加载中',
    })
    app.util.request({
      url: 'api/deal/findState',
      method: "post",
      data: {
        state:_this.data.active,
        page:_this.data.pageNum,
        pageSize:10,
      },
      success(response) {
        wx.hideLoading()
          if (response.data.code == 0) {
            _this.setData({
              pageTotal:response.data.data[0].deal.pages,
              good_list:[..._this.data.good_list,...response.data.data[0].deal.list]
            })
            console.log(_this.data.good_list)
          }
      }
    })
  },
  saveRemark(e){
    let _this = this
    app.util.request({
      url:'api/account/addRemark',
      method:'post',
      data:{
        remark:e.detail
      },success(response){
        if (response.data.code == 0) {
         wx.showToast({
           title: '保存成功',
         })
        }
      }
    })
  },
  toAddTrading(){
    if(this.data.active==2)
    wx.switchTab({
      url: '/pages/market/market-index',
    })
    else{
    wx.navigateTo({
      url: '/pages/goods/my-goods-index',
    })
  }
  },
  showDialog(){
    this.setData({
      showConfirm:true
    })
  },
  // 取消交易
  toCancelTrading(){
    let _this = this
    if(_this.data.result.length==0){
      wx.showToast({
        icon:'none',
        title: '请选择一项操作',
      })
    }else if(_this.data.active==2){
      _this.cancelWantGet().then(res =>{
        _this.refundDeal()
      })
    }else{
      app.util.request({
        url:'api/deal/delete',
        method:'post',
        data:{
          id:_this.data.result
        },success(response){
          if (response.data.code == 0) {
            // _this.data.good_list=[]
            _this.setData({
              good_list:[]
            })
            _this.getTradingList()
          }
        }
      })
    }
    _this.setData({
      showConfirm:false
    })
  },
  // 取消求购订单
  cancelWantGet(){
    return new Promise(resolve => {
      app.util.request({
        url:'api/deal/refund',
        method:'post',
        data:{
          id:this.data.result
        },success(response){
          if (response.data.code == 0) {
            resolve()
          }else{
            wx.showToast({
              icon:'none',
              title:response.data.msg
            })
          }
        }
      })
    })
  },
  // 退款
  refundDeal(){
    let _this = this
    app.util.request({
      url:'wxPay/refund',
      method:'post',
      data:{
        id:_this.data.result
      },success(response){
        if (response.data.code == 0) {
          _this.getTradingList()
        }else{
          wx.showToast({
            icon:'none',
            title:response.data.msg
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.pageNum<this.data.pageTotal){
      this.data.pageNum++
      this.getTradingList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
